package com.platform.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.platform.entity.PostsPictureEntity;
import com.platform.service.PostsPictureService;
import com.platform.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.PostsEntity;
import com.platform.service.PostsService;

import javax.servlet.http.HttpServletRequest;

/**
 * 春芽社区故事Controller
 *
 * @author czj
 * @email 729618941@qq.com
 * @date 2018-11-03 11:24:17
 */
@RestController
@RequestMapping("posts")
public class PostsController {
    @Autowired
    private PostsService postsService;
    @Autowired
    private PostsPictureService postsPictureService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("posts:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<PostsEntity> postsList = postsService.queryList(query);
        int total = postsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(postsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("posts:info")
    public R info(@PathVariable("id") Integer id) {
        PostsEntity posts = postsService.queryObject(id);

        return R.ok().put("posts", posts);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("posts:save")
    public R save(@RequestBody PostsEntity posts, HttpServletRequest request) throws UnsupportedEncodingException {
        String ip = RequestUtil.getIpAddrByRequest(request);
        posts.setUserId(86);
        posts.setIp(ip);
        posts.setCity("深圳");
        posts.setViewCount(0);
        posts.setCollectCount(0);
        posts.setLikeCount(0);
        posts.setAddTime(new Date());
        posts.setTitle(EmojiUtils.emojiConvert1(posts.getTitle()));
        posts.setContentDesc(EmojiUtils.emojiConvert1(posts.getContentDesc()));
        //todo 处理置顶,把之前的置顶的取消置顶
        if (posts.getIsTop() == 1) {
            postsService.updateTopPosts();
        }
        postsService.save(posts);
        Integer postsId = posts.getId();
        //保存图片
        List<PostsPictureEntity> postImgList = posts.getPostImgList();
        if (postImgList.size() > 0) {
            int i = 1;
            for (PostsPictureEntity postsPictureEntity : postImgList) {
                postsPictureEntity.setPostId(postsId);
                postsPictureEntity.setSortOrder(i);
                postsPictureService.save(postsPictureEntity);
                i++;
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("posts:update")
    public R update(@RequestBody PostsEntity posts) {
        Integer postsId = posts.getId();

        List<PostsPictureEntity> postImgList = posts.getPostImgList();
        if (postImgList.size() > 0) {
            // 先删除之前的图片,再重新添加
            postsPictureService.deleteByPostId(postsId);

            int i = 1;
            for (PostsPictureEntity postsPictureEntity : postImgList) {
                postsPictureEntity.setPostId(postsId);
                postsPictureEntity.setSortOrder(i);
                postsPictureService.save(postsPictureEntity);
                i++;
            }
        }

        //todo 处理置顶,把之前的置顶的取消置顶
        if (posts.getIsTop() == 1) {
            postsService.updateTopPosts();
        }

        postsService.update(posts);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("posts:delete")
    public R delete(@RequestBody Integer[] ids) {
        postsService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<PostsEntity> list = postsService.queryList(params);

        return R.ok().put("list", list);
    }
}
